<?php
/**
 * Created by PhpStorm.
 * User: clkj1
 * Date: 2018/7/13
 * Time: 9:41
 */

class SqlS
{
    protected $conn;
    protected $res;

    function __construct()
    {
        // 1.链接数据库
        if ($_SERVER['HTTP_HOST'] == 'poster.hdacgn.com') $passwd = '1935201a7515b830'; else $passwd = 'root';
        $conn = @new mysqli('localhost', 'root', $passwd, 'poster_system');
        if ($conn->connect_error) {//返回链接错误号
            // 返回链接错误信息
            die('数据库链接错误 (' . $conn->connect_errno . ') '
                . $conn->connect_error);
        }
        // 2. 选择数据库
        $conn->select_db("poster_system") or die("选择数据库失败：" . $conn->error);
        $conn->set_charset('utf8');
        $conn->query("set character set 'utf8'");
        $conn->query("set names 'utf8'");
        $this->conn = $conn;
    }

    public function showQuestions()
    {
        // 默认问题：
        $sql = 'SELECT pk_questions_id, qs_Name, qs_Type FROM tbl_questions WHERE  qs_isShow = 1 AND qs_CreateUid = 1 ORDER BY pk_questions_id ASC';
        //选择的记录
        if (isset($_SESSION['listArray'])) {
            $selected_array = $_SESSION['listArray'];
        } else {
            $selected_array = [[], []];
        }
        // var_dump($selected_array);
        $res = $this->conn->query($sql);
        $num = $res->num_rows;
        for ($i = 0; $i < $num; $i++) {
            $row = $res->fetch_row();
            if ($i == 0 || in_array($row[1], $selected_array[0])) {
                if ($row[2] == 2) {
                    ?>
                    <div onclick="ajax_selectCourse(<?= $row[0] ?>,<?= $i ?>)">
                        <li class="active"><?= $row[1] ?></li>
                    </div>
                    <?php
                } else {
                    ?>
                    <li class="active"><?= $row[1] ?></li>
                    <?php
                }
            } else {
                if ($row[2] == 2) {
                    ?>
                    <div onclick="ajax_selectCourse(<?= $row[0] ?>,<?= $i ?>)">
                        <li><?= $row[1] ?></li>
                    </div>
                    <?php
                } else {
                    ?>
                    <li><?= $row[1] ?></li>
                    <?php
                }
            }
        }
        // 用户自定义的问题：
        if ($_SESSION['uid'] != 1) {
            $uid = $_SESSION['uid'];
            $sql = "SELECT pk_questions_id, qs_Name, qs_isShow FROM tbl_questions WHERE  qs_CreateUid = $uid ORDER BY pk_questions_id ASC";
            $res = $this->conn->query($sql);
            $num = $res->num_rows;
            for ($i = 0; $i < $num; $i++) {
                $row = $res->fetch_row();
                if (in_array($row[1], $selected_array[1])) {
                    ?>
                    <div onclick="ajax_selectDiyCourse(<?= $row[0] ?>, <?= $i ?>)">
                        <li class="active"><?= $row[1] ?></li>
                    </div>
                    <?php
                } else {
                    ?>
                    <div onclick="ajax_selectDiyCourse(<?= $row[0] ?>, <?= $i ?>)">
                        <li><?= $row[1] ?></li>
                    </div>
                    <?php
                }
            }
        }
    }

    public function courseWrite($id, $uid)
    {
        $id = addslashes($id);
        $uid = addslashes($uid);
        //查出选择的是什么内容：
        $sql = "SELECT qs_Modes, qs_DefaultText, qs_Name FROM tbl_questions WHERE pk_questions_id = $id";
        $res = $this->conn->query($sql);
        $row = $res->fetch_row();
        array_push($_SESSION['listArray'][0], $row[2]);
        //查出已经写过的数据
        $sql_content = "SELECT qc_QuContent FROM tbl_question_content WHERE qc_userId = $uid";
        $res_content = $this->conn->query($sql_content);
        $row_content = $res_content->fetch_row();
        if ($res_content) {
            $write_array = json_decode($row_content[0], true);
        } else {
            $write_array = array();
        }
        $num = count($write_array);
        for ($i = 0; $i < 10; $i++) {
            if ($i < $num) {
                ?>
                <li>
                    <span><?= $i + 1 ?></span>
                    <input type="text" placeholder="<?= $row[1] ?>" value="<?= $write_array[$i] ?>">
                </li>
                <?php
            } else {
                ?>
                <li>
                    <span><?= $i + 1 ?></span>
                    <input type="text" placeholder="<?= $row[1] ?>">
                </li>
                <?php
            }
        }
    }

    public function addCourses($json, $id, $uid)
    {
        $uid = addslashes($uid);
        $id = addslashes($id);
        $json = addslashes($json);
        $sql = "DELETE FROM tbl_question_content WHERE qc_userId = $uid AND qc_QuId = $id";
        $this->conn->query($sql);
        $sql = "INSERT INTO tbl_question_content (qc_userId, qc_QuId, qc_QuContent)VALUES($uid, $id, '$json')";
        $this->conn->query($sql);
    }

    public function seSelectDiyQuCo($qId)
    {
        $qId = addslashes($qId);
        $sql = "SELECT qs_Name, qs_isShow, qs_Type, qs_Modes FROM tbl_questions WHERE pk_questions_id = $qId";
        $res = $this->conn->query($sql);
        return $res->fetch_row();
    }

    public function inputDiyQuestion($qId, $qName, $qShow, $qType, $qTexts, $uid)
    {
        $qName = addslashes(trim($qName));
        $qId = addslashes($qId);
        $qShow = addslashes($qShow);
        $qType = addslashes($qType);
        $qTexts = addslashes($qTexts);
        $uid = addslashes($uid);
        if ($qId == 'false') {
            // 如果没有问题id那么是新建问题
            $sql = "INSERT INTO tbl_questions(`qs_CreateUid`, `qs_Name`, `qs_isShow`, `qs_Type`, `qs_Modes`)VALUES($uid, '$qName', $qShow, '$qType', '$qTexts')";
        } else {
            // 有问题id就更新问题
            $sql = "UPDATE tbl_questions SET qs_Name = '$qName', qs_isShow = $qShow, qs_Type = '$qType', qs_Modes = '$qTexts' WHERE pk_questions_id = $qId";
        }
        $res = $this->conn->query($sql);
        if ($res) {
            array_push($_SESSION['listArray'][1], $qName);
            return true;
        } else
            return false;
    }

    /**
     * 作成表格：
     * @param string $quName
     * @return mixed
     */
    public function buildDeQuestion($quName)
    {
        $quName = addslashes($quName);
        $sql = "SELECT pk_questions_id, qs_Type, qs_Modes, qs_DefaultText FROM tbl_questions WHERE qs_Name = '$quName' AND qs_CreateUid = 1";
        $res = $this->conn->query($sql);
        $row = $res->fetch_row();
        return $row;
    }

    public function searchQuContent($uid, $qid)
    {
        $uid = addslashes($uid);
        $qid = addslashes($qid);
        $sql = "SELECT qc_QuContent FROM tbl_question_content WHERE qc_userId = $uid AND qc_QuId = $qid";
        $res = $this->conn->query($sql);
        $row = $res->fetch_row();
        return json_decode($row[0], true);
    }

    public function searchDiShowQuContent($quName, $uid)
    {
        $quName = addslashes($quName);
        $uid = addslashes($uid);
        $sql = "SELECT qs_Type, qs_Modes, qs_isShow FROM tbl_questions WHERE qs_CreateUid = $uid AND qs_Name = '$quName' ORDER BY pk_questions_id ASC";
        $res = $this->conn->query($sql);
        $row = $res->fetch_row();
        return $row;
    }

    // 做成表格
    public function makeQrCodeNow($uid, $array, $fid, $title)
    {
        $title = addslashes($title);
        $fid = addslashes($fid);
        $title = addslashes($title);
        $time = time();
        // 是修改表还是创建表：
        $sql_hasForm = "SELECT 1 FROM tbl_tables WHERE ts_UserId = $uid AND ts_TaId = $fid";
        $res = $this->conn->query($sql_hasForm);
        if ($res->num_rows == 0)
            $hadForm = false;
        else
            $hadForm = true;
        // 如果已经有了，删除所有，重新写入
        if ($hadForm) {
            $sql_del_form = "DELETE FROM tbl_tables WHERE ts_UserId = $uid AND ts_TaId = $fid";
            $this->conn->query($sql_del_form);
        }
        // 插入默认问题
        list($de_array, $di_array) = $array;
        $de_count = count($de_array);
        for ($i = 0; $i < $de_count; $i++) {
            // 查出所有选中的默认问题
            $qName = addslashes($de_array[$i]);
            $sql = "SELECT pk_questions_id, qs_Type, qs_Modes, qs_DefaultText FROM tbl_questions WHERE qs_CreateUid = 1 AND qs_isShow = 1 AND qs_Name = '$qName'";
            $res = $this->conn->query($sql);
            if (($row = $res->fetch_row()) != false) {
                list($qId, $qType, $qModes, $qBaText) = $row;
                if ($qType == 2) {
                    // 如果是课程多选，需要有自定义多选内容
                    $sql_choose_things = "SELECT qc_QuContent FROM tbl_question_content WHERE qc_userId = $uid AND qc_QuId = $qId";
                    $res_choose = $this->conn->query($sql_choose_things);
                    $rows = $res_choose->fetch_row();
                    $d_qModes = addslashes($rows[0]);
                    $qBaText = addslashes($qBaText);
                    $sql_insert = "INSERT INTO tbl_tables(ts_UserId, ts_TaId, ts_QuName, ts_QuType, ts_QuModes, ts_QuBaText, ts_QuCt, ts_title)VALUES($uid, $fid, '$qName', $qType, '$d_qModes', '$qBaText', $time, '$title')";
                    $this->conn->query($sql_insert);
                } else {
                    $qModes = addslashes($qModes);
                    $qBaText = addslashes($qBaText);
                    $sql_insert = "INSERT INTO tbl_tables(ts_UserId, ts_TaId, ts_QuName, ts_QuType, ts_QuModes, ts_QuBaText, ts_QuCt, ts_title)VALUES($uid, $fid, '$qName', $qType, '$qModes', '$qBaText', $time, '$title')";
                    $this->conn->query($sql_insert);
                }
            }
        }
        // 插入自定义问题：
        // 查询自定义启用的问题：
        $di_count = count($di_array);
        for ($i = 0; $i < $di_count; $i++) {
            // 查出所有选中的自定义问题
            $qName_diy = addslashes($di_array[$i]);
            $sql = "SELECT qs_isShow, qs_Type, qs_Modes, qs_DefaultText FROM tbl_questions WHERE qs_CreateUid = $uid AND qs_Name = '$qName_diy'";
            $res = $this->conn->query($sql);
            if (($row = $res->fetch_row()) != false) {
                list($qShow, $qType, $qModes, $qBaText_diy) = $row;
                if ($qShow == 1) {
                    $qType = 3;
                    $qModes = '["25"]';
                }
                $qModes = addslashes($qModes);
                if (empty($qBaText_diy)) {
                    $qBaText_diy = '请填写' . $qName_diy;
                }
                $qBaText_diy = addslashes($qBaText_diy);
                $sql_insert = "INSERT INTO tbl_tables(ts_UserId, ts_TaId, ts_QuName, ts_QuType, ts_QuModes, ts_QuBaText, ts_QuCt, ts_title)VALUES($uid, $fid, '$qName_diy', $qType, '$qModes', '$qBaText_diy', $time, '$title')";
                $this->conn->query($sql_insert);
            }
        }
    }

    /**
     * 查询默认图片
     * @param int $type 图片类型 目前 1 背景， 2 标题
     * @return array|bool
     */
    public function listDeImage($type)
    {
        $type = addslashes($type);
        $sql = "SELECT di_imUrl_a, di_imUrl_b FROM tbl_default_images WHERE di_imType = $type ORDER BY pk_default_images_id ASC";
        $res = $this->conn->query($sql);
        $num_rows = $res->num_rows;
        if ($num_rows > 0) {
            $list_a = array();
            $list_b = array();
            for ($i = 0; $i < $num_rows; $i++) {
                $row_array = $res->fetch_row();
                $list_a[] = $row_array[0];
                $list_b[] = $row_array[1];
            }
            return array($list_a, $list_b);
        } else {
            return false;
        }
    }

    /**
     * 把默认图片链接转为id
     * @param string $url
     * @param int $type ：1 背景 2 标题 3 图片版式
     * @return int
     */
    public function changeDeImageTextToId($url, $type)
    {
        $url = addslashes($url);
        $type = addslashes($type);
        $sql = "SELECT di_imUrl_a FROM tbl_default_images WHERE di_imType = $type ORDER BY pk_default_images_id ASC";
        $res = $this->conn->query($sql);
        $num_rows = $res->num_rows;
        for ($i = 0; $i < $num_rows; $i++) {
            $row_array = $res->fetch_row();
            if ($row_array[0] == $url) {
                return $i;
            }
        }
    }

    // 把前端图片转后端图片：
    public function changeAImToBIm($im_a, $type)
    {
        $im_a = addslashes(trim($im_a));
        $type = addslashes(trim($type));
        $sql = "SELECT di_imUrl_b, di_diy_x, di_diy_y FROM tbl_default_images WHERE di_imUrl_a = '$im_a' AND di_imType = '$type'";
        $res = $this->conn->query($sql);
        $row = $res->fetch_row();
        return $row;
    }

    /**
     * 查询已经做好的表格信息
     * @param int $uid
     * @param int $fid
     * @return array
     */
    public function searchFormInformation($uid, $fid)
    {
        $uid = addslashes(trim($uid));
        $fid = addslashes(trim($fid));
        $sql = "SELECT ts_QuName, ts_QuType, ts_QuModes, ts_QuBaText FROM tbl_tables WHERE ts_UserId = '$uid' AND ts_TaId = '$fid' ORDER BY pk_tables_id";
        $res = $this->conn->query($sql);
        $num_rows = $res->num_rows;
        $rows = array();
        for ($i = 0; $i < $num_rows; $i++) {
            $rows[] = $res->fetch_row();
        }
        $sql = "SELECT DISTINCT ts_title FROM tbl_tables WHERE ts_UserId = '$uid' AND ts_TaId = '$fid'";
        $res = $this->conn->query($sql);
        $row = $res->fetch_row();
        $title = $row[0];
        return array($num_rows, $rows, $title);
    }

    // 填表：
    public function addForm($array, $cid, $fid)
    {
        $cid = addslashes($cid);
        $fid = addslashes($fid);
        $time = time();
        while (true) {
            $uid = $this->fileReName(0, 5);
            $sql_qc = "SELECT 1 FROM tbl_table_content WHERE tc_UserId = $uid AND tc_CId = $cid AND tc_TaId = $fid";
            $res = $this->conn->query($sql_qc);
            $row = $res->fetch_row();
            if ($row[0] < 1) {
                break;
            }
        }
        $text_array = $array[0];
        $radio_array = $array[1];
        $box_array = $array[2];
        $m = 0;
        // 把数据和表格进行核对 ++++++++++++++++++++++++++++++++++++
        //$sql_tale = "SELECT  FROM WHERE";
        for ($i = 0; $i < count($text_array); $i++) {
            $m++;
            $text_name = addslashes($text_array[$i][0]);
            $text_answer = addslashes($text_array[$i][1]);
            if ($text_name == '家长姓名' || $text_name == '学生姓名') {
                if (!preg_match_all('/^[\x{4E00}-\x{9FA5}\x{f900}-\x{fa2d}·s]{2,20}$/u', $text_answer)) {
                    return array(1, "不是正确的{$text_name}", $text_name);
                }
            }
            if ($text_name == '家长手机') {
                if (!preg_match_all('/^1[3|4|5|8]\d{9}$/', $text_answer)) {
                    return array(1, '不是完整的11位手机号或者正确的手机号前七位', $text_name);
                }
            }
            if ($text_name == '出生日期') {
                preg_match_all('/(\d+)/', $text_answer, $bir_array);
                if ($bir_array != null) {
                    if (count($bir_array[0]) == 3) {
                        $trueDate = date('Y-m-d', time());
                        preg_match_all('/(\d+)/', $trueDate, $day_array);
                        $trueYear = (int)$day_array[0][0];
                        $trueMonth = (int)$day_array[0][1];
                        $trueDay = (int)$day_array[0][2];
                        $birYear = (int)$bir_array[0][0];
                        $birMonth = (int)$bir_array[0][1];
                        $birDay = (int)$bir_array[0][2];
                        if ($birYear > $trueYear) {
                            return array(1, '错误的出生年份', $text_name);
                        }
                        if ($birYear === $trueYear) {
                            if ($birMonth > $trueMonth) {
                                return array(1, '错误的出生月份', $text_name);
                            }
                            if ($birMonth == $trueMonth) {
                                if ($birDay > $trueDay) {
                                    return array(1, '错误的出生日', $text_name);
                                }
                            }
                        }
                        if ($birMonth == 1 || $birMonth == 3 || $birMonth == 5 || $birMonth == 7 || $birMonth == 8 || $birMonth == 10 || $birMonth == 11 || $birMonth == 12) {
                            if ($birDay > 31) {
                                return array(1, '错误的出生日', $text_name);
                            }
                        }
                        if ($birMonth === 4 || $birMonth === 6 || $birMonth === 9 || $birMonth === 11) {
                            if ($birDay > 30) {
                                return array(1, '错误的出生日', $text_name);
                            }
                        }
                        if ($birMonth === 2) {
                            if ($birYear % 4 === 0 || $birYear % 100 === 0) {
                                if ($birDay > 29) {
                                    return array(1, '错误的出生日', $text_name);
                                }
                            } else {
                                if ($birDay > 28) {
                                    return array(1, '错误的出生日', $text_name);
                                }
                            }
                        }
                    } else {
                        return array(1, '出生日期没有填写完整', $i);
                    }
                } else {
                    return array(1, '不标准的出生日期', $i);
                }
            }
            $text_name = addslashes($text_name);
            $text_answer = addslashes(json_encode(array($text_answer), JSON_UNESCAPED_UNICODE));
            $sql = "INSERT INTO tbl_table_content(tc_UserId, tc_CId, tc_TaId, tc_QuId, tc_QuType, tc_QuName, tc_QuAnswer, tc_QuCt)VALUES($uid, $cid, $fid, $m, 1, '$text_name', '$text_answer', $time)";
            $this->conn->query($sql);
        }
        for ($i = 0; $i < count($radio_array); $i++) {
            $m++;
            $text_name = addslashes($radio_array[$i][0]);
            if (count($radio_array) == 0) {
                return array(2, "{$radio_array[$i][0]}没有选择", $radio_array[$i][0]);
            }
            $text_answer = addslashes(json_encode(array($radio_array[$i][1]), JSON_UNESCAPED_UNICODE));
            $sql = "INSERT INTO tbl_table_content(tc_UserId, tc_CId, tc_TaId, tc_QuId, tc_QuType, tc_QuName, tc_QuAnswer, tc_QuCt)VALUES($uid, $cid, $fid, $m, 2, '$text_name', '$text_answer', $time)";
            $this->conn->query($sql);
        }
        for ($i = 0; $i < count($box_array); $i++) {
            $m++;
            $text_name = addslashes($box_array[$i][0]);
            if (count($box_array) == 0) {
                return array(3, "{$box_array[$i][0]}没有选择", $box_array[$i][0]);
            }
            $text_answer = addslashes(json_encode($box_array[$i][1], JSON_UNESCAPED_UNICODE));
            $sql = "INSERT INTO tbl_table_content(tc_UserId, tc_CId, tc_TaId, tc_QuId, tc_QuType, tc_QuName, tc_QuAnswer, tc_QuCt)VALUES($uid, $cid, $fid, $m, 3, '$text_name', '$text_answer', $time)";
            $this->conn->query($sql);
        }
        return array(0, $uid, 0);
    }

    // 添加图片
    public function addTimeOutImages($uid, $file, $t = 0)
    {
        $uid = addslashes($uid);
        $file = addslashes($file);
        $time = time();
        if ($t = 0) {
            $outTime = $time + 1209600;// (14*24*3600)
        } elseif ($t = 1) {
            $outTime = $time + 5184000;// 60*..
        }
        $sql = "INSERT INTO tbl_image_recycle_bin(qrb_userId, qrb_imageUrl, qrb_createTime, qrb_deleteTime)VALUES($uid, '$file', $time, $outTime)";
        $this->conn->query($sql);
        $this->delTimeOutImages();
    }

    // 清理过期图片
    public function delTimeOutImages()
    {
        $time = time();
        $sql = "SELECT qrb_imageUrl FROM tbl_image_recycle_bin WHERE qrb_deleteTime < $time";
        $res = $this->conn->query($sql);
        if (!$res) {
            echo $this->conn->error;
        }
        $num = $res->num_rows;
        for ($i = 0; $i < $num; $i++) {
            $row = $res->fetch_row();
            $url = $row[0];
            $sql_search = "SELECT COUNT(1) FROM tbl_tables WHERE ts_PoIm = '$url'";
            $res_search = $this->conn->query($sql_search);
            if ($res_search) {
                $hm_a = $res_search->fetch_row();
                if ($hm_a[0] < 1) {
                    unlink($url);
                    $sql = "DELETE FROM tbl_image_recycle_bin WHERE qrb_imageUrl = '$url'";
                    $re = $this->conn->query($sql);
                    if (!$re) {
                        echo $this->sqlError();
                    }
                }
            } else echo $this->sqlError();
        }
    }

    // 查询某用户的问题：
    public function CPUidForm($cid, $pid, $uid)
    {
        $cid = addslashes($cid);
        $pid = addslashes($pid);
        $uid = addslashes($uid);
        $sql = "SELECT tc_QuName, tc_QuAnswer, tc_QuType FROM tbl_table_content WHERE tc_CId = $cid AND tc_TaId = $pid AND tc_UserId = $uid";
        $res = $this->conn->query($sql);
        if ($res) {
            $num = $res->num_rows;
            $result_array = array();
            for ($i = 0; $i < $num; $i++) {
                $row = $res->fetch_row();
                $result_array[] = array($row[0], $row[1], $row[2]);
            }
        } else {
            $num = 0;
            $result_array = array();
        }
        return array($num, $result_array);
    }

    /**
     * 分割 session temp e.g. "123|123|123|159|"
     * @param string $temp
     * @return array
     */
    public function explodeTemp($temp)
    {
        $temp_array = explode('|', $temp);
        $length = count($temp_array);
        // 删除所有空白
        for ($i = 0; $i < $length; $i++) {
            $key = array_search('', $temp_array);
            if ($key !== false)
                array_splice($temp_array, $key, 1);
        }
        $length = count($temp_array);
        return array($length, $temp_array);
    }

    /**
     * 随机数命名
     * @param int $size 复杂程度
     * @param int $length 长度
     * @return string
     */
    public function fileReName($size = 6, $length = 10)
    {
        //创建随机数，用来给二维码命名
        $numbers = range(0, 9);
        $letters_s = range('a', 'z');
        $letters_b = range('A', 'Z');
        switch ($size) {
            case 0:
                $changeStr = join('', $numbers);
                break;
            case 1:
                $changeStr = join('', $letters_s);
                break;
            case 2:
                $changeStr = join('', $letters_b);
                break;
            case 3:
                $changeStr = join("", array_merge($numbers, $letters_s));
                break;
            case 4:
                $changeStr = join("", array_merge($numbers, $letters_b));
                break;
            case 5:
                $changeStr = join("", array_merge($letters_s, $letters_b));
                break;
            case 6:
                $changeStr = join('', array_merge($numbers, $letters_s, $letters_b));
                break;
            default :
                $changeStr = join('', array_merge($numbers, $letters_s, $letters_b));
        }
        $outName = str_shuffle($changeStr){0};
        for ($i = 0; $i < $length; $i++) {
            $outName .= str_shuffle($changeStr){0};
        }
        return $outName;
    }

    public function addImInTa($cid, $tid, $im)
    {
        $cid = addslashes($cid);
        $tid = addslashes($tid);
        $im = addslashes($im);
        if ($cid != 1) {
            $sql_delete_diy_questions = "DELETE FROM tbl_questions WHERE qs_CreateUid = '$cid'";
            $this->conn->query($sql_delete_diy_questions);
            $sql_delete_diy_questions = "DELETE FROM tbl_question_content WHERE qc_userId = '$cid'";
            $this->conn->query($sql_delete_diy_questions);
        }
        $f_sql = "SELECT COUNT(1) FROM tbl_tables WHERE ts_UserId = '$cid' AND ts_TaId = '$tid'";
        $f_res = $this->conn->query($f_sql);
        if ($f_res) {
            $f_row = $f_res->fetch_row();
            if ($f_row[0] > 0) {
                $sql = "UPDATE tbl_tables SET ts_PoIm = '$im' WHERE ts_UserId = $cid AND ts_TaId = $tid";
                $res = $this->conn->query($sql);
                if ($res) {
                    return 0;
                } else {
                    return 1;
                }
            } else {
                $time = time();
                $sql = "INSERT INTO tbl_tables(ts_UserId, ts_TaId, ts_QuName, ts_QuType, ts_QuModes, ts_QuBaText, ts_QuCt, ts_PoIm)VALUES($cid, $tid, '无', '0', '0', '', $time, '$im')";
                $res = $this->conn->query($sql);
                if ($res) {
                    return 0;
                } else {
                    return 1;
                }
            }
        } else return $this->conn->error;
    }

    // 根据uid查出set信息
    public function searchUidForSet($uid)
    {
        $uid = addslashes($uid);
        $sql = "SELECT us_sets FROM tbl_users WHERE pk_users_id = $uid";
        $res = $this->conn->query($sql);
        if ($res) {
            $row = $res->fetch_row();
            return $row[0];
        } else {
            return $this->sqlError();
        }
    }

    // 删除diy问题
    public function deleteDitQuestion($uid, $quId)
    {
        $uid = addslashes($uid);
        $quId = addslashes($quId);
        $sql = "DELETE FROM tbl_questions WHERE qs_CreateUid = $uid AND pk_questions_id = $quId";
        $res = $this->conn->query($sql);
        return $res ? 0 : 1;
    }

    // error
    private function sqlError()
    {
        return $this->conn->error;
    }

    function __destruct()
    {
        // TODO: Implement __destruct() method.
        // 10.释放结果集资源
        if (isset($this->res) && !is_bool($this->res))
            $this->res->close();// $res->free();
        // 关闭数据库连接
        $this->conn->close();
    }
}